package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Intervention;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.InterventionService;
import com.itheima.service.OrderService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/orderlist")
public class OrderListController {
    @Reference
    private OrderService orderService;

    @Reference
    private InterventionService interventionService;
    /**
     * 分页查询所有数据进行回显
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = null;
        try {
            pageResult = orderService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }

    /**
     * 添加预约信息
     *
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Map map, Integer setmealId) {
        try {
            Result result = orderService.add(map, setmealId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ORDER_FAIL);
        }
    }

    /**
     * 修改预约信息
     *
     * @param map
     * @param setmealId
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Map map, Integer setmealId) {
        try {
            orderService.update(map, setmealId);
        } catch (Exception e) {
            return new Result(false, MessageConstant.UPDATE_ORDER_FAIL);
        }
        return new Result(true, MessageConstant.UPDATE_ORDER_SUCCESS);
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        try {
            orderService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_ORDER_SUCCESS);
    }

    /**
     * 根据ID查询，用于编辑框数据回显
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map map = orderService.findByOrderId(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 根据ID查询套餐信息，用于回显
     *
     * @param id
     * @return
     */
    @RequestMapping("/findSetmealById")
    public Result findSetmealById(Integer id) {
        try {
            Setmeal setmeal = orderService.findSetmealById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 一键修改预约状态
     *
     * @return
     */
    @RequestMapping("/changeStatus")
    public Result changeStatus(@RequestBody Map<String, Object> map) {
        try {
            orderService.changeStatus(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ORDERLIST_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ORDERLIST_SUCCESS);
    }

    /**
     * 根据订单id添加一个医生建议
     * @param intervention
     * @param foodIds
     * @param sportIds
     * @return
     */
    @RequestMapping("/addAdvice")
    public Result addAdvice(@RequestBody Intervention intervention, Integer[] foodIds, Integer[] sportIds){
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        intervention.setDoctorName(username);
        try {
            interventionService.addAdvice(intervention,foodIds,sportIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"已添加!无法重复添加!");
        }
        return new Result(true,MessageConstant.ADD_INTERVENTION_SUCCESS);
    }

}
